﻿using IronPython.Runtime.Operations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.GraphicDemonstrator
{
    public class DemoResourceInfo
    {
        public string LinkTitle { get; set; }

        public string LinkType { get; set; }

        public string RelativePath { get; set; }

        private string absolutePath = "";

        public string AbsolutePath
        {
            get
            {
                if (string.IsNullOrWhiteSpace(absolutePath) == false) return absolutePath;

                if (string.IsNullOrWhiteSpace(RelativePath))
                {
                    if (Globals.MainWindow.tvWorkDirectory.Items.Count <= 0) return "";

                    var rootWtvi = Globals.MainWindow.tvWorkDirectory.Items[0] as WorkspaceTreeViewItem;
                    if (rootWtvi == null) return "";

                    return rootWtvi.MetaHtmlFilePath;
                }

                var path = Globals.PathOfWorkspace.TrimEnd('\\') + "\\" + RelativePath.TrimStart('\\');

                if (System.IO.File.Exists(path)) return path;

                try
                {
                    if (System.IO.Directory.Exists(path))
                    {
                        var di = new System.IO.DirectoryInfo(path);
                        path = di.FullName;
                        if (path.Length > 0 && path[path.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                        {
                            path += System.IO.Path.DirectorySeparatorChar;
                        }
                        return path + "_" + di.Name + ".html";
                    }
                    else return "";
                }
                catch (Exception ex)
                {
                    LMessageBox.ShowWarning(ex.Message);
                    return "";
                }
            }
            set { this.absolutePath = value; }
        }
    }
}
